package cn.com.anypay.manager.payment.config;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试渠道配置类
 *
 * 用于测试渠道的配置管理
 *
 * @author AnyPay Team
 * @version 1.0
 * @since 2025-07-05
 */
public class TestChannelConfig implements ChannelConfig {

    private String channelId;
    private String channelName;
    private boolean enabled;
    private String environment;
    private Map<String, Object> configParams;

    public TestChannelConfig(String channelId, String channelName) {
        this.channelId = channelId;
        this.channelName = channelName;
        this.enabled = true;
        this.environment = "test";
        this.configParams = new HashMap<>();

        // 设置默认配置
        configParams.put("timeout", 30000);
        configParams.put("retryCount", 3);
        configParams.put("logLevel", "INFO");
        configParams.put("mockMode", true);
    }

    @Override
    public String getChannelId() {
        return channelId;
    }

    @Override
    public String getChannelName() {
        return channelName;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public Map<String, Object> getConfigParams() {
        return configParams;
    }

    @Override
    public Object getConfigParam(String key) {
        return configParams.get(key);
    }

    @Override
    public String getConfigParamAsString(String key) {
        Object value = getConfigParam(key);
        return value != null ? value.toString() : null;
    }

    @Override
    public Integer getConfigParamAsInteger(String key) {
        Object value = getConfigParam(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        try {
            return Integer.parseInt(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Override
    public Boolean getConfigParamAsBoolean(String key) {
        Object value = getConfigParam(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        return Boolean.parseBoolean(value.toString());
    }

    @Override
    public boolean isValid() {
        // 验证必要的配置参数
        if (channelId == null || channelId.trim().isEmpty()) {
            return false;
        }

        if (channelName == null || channelName.trim().isEmpty()) {
            return false;
        }

        if (configParams == null) {
            return false;
        }

        // 验证超时时间
        Integer timeout = getConfigParamAsInteger("timeout");
        if (timeout == null || timeout <= 0) {
            return false;
        }

        // 验证重试次数
        Integer retryCount = getConfigParamAsInteger("retryCount");
        if (retryCount == null || retryCount < 0) {
            return false;
        }

        return true;
    }

    @Override
    public String[] getValidationErrors() {
        if (isValid()) {
            return new String[0];
        }

        java.util.List<String> errors = new java.util.ArrayList<>();

        if (channelId == null || channelId.trim().isEmpty()) {
            errors.add("Channel ID is null or empty");
        }

        if (channelName == null || channelName.trim().isEmpty()) {
            errors.add("Channel name is null or empty");
        }

        if (configParams == null) {
            errors.add("Config params is null");
        } else {
            Integer timeout = getConfigParamAsInteger("timeout");
            if (timeout == null || timeout <= 0) {
                errors.add("Timeout is null or invalid");
            }

            Integer retryCount = getConfigParamAsInteger("retryCount");
            if (retryCount == null || retryCount < 0) {
                errors.add("Retry count is null or invalid");
            }
        }

        return errors.toArray(new String[0]);
    }

    @Override
    public String getEnvironment() {
        return environment;
    }

    // Setters for configuration
    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setEnvironment(String environment) {
        this.environment = environment;
    }

    public void setConfigParams(Map<String, Object> configParams) {
        this.configParams = configParams;
    }

    public void setConfigParam(String key, Object value) {
        if (configParams == null) {
            configParams = new HashMap<>();
        }
        configParams.put(key, value);
    }

    @Override
    public String toString() {
        return "TestChannelConfig{" +
                "channelId='" + channelId + '\'' +
                ", channelName='" + channelName + '\'' +
                ", enabled=" + enabled +
                ", environment='" + environment + '\'' +
                ", configParams=" + configParams +
                '}';
    }
}